package com.leaf.lowcode.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.leaf.lowcode.API.RootApi;
import com.leaf.lowcode.entity.RootMapList;
import com.leaf.lowcode.utils.JsontoBean;
import com.leaf.lowcode.utils.RegularInstance;
import com.tencent.welink.iot.SubDeviceDTO;
import com.tencent.welink.iot.WelinkHttp;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RootSendUp {
 // @Scheduled(initialDelay = 20*1000,fixedRate = 15*1000)
    public void execute() {
        List<SubDeviceDTO> subDeviceDTOList = RegularInstance.subDeviceDTOList;
        if(subDeviceDTOList!=null) {
            for (SubDeviceDTO dto : subDeviceDTOList) {
                String pid = dto.getPid();
                String sn = dto.getSn();
                String din = dto.getDin();
                if ("1701001538".equalsIgnoreCase(pid)) {
                  System.out.println("机器人");
                    RootApi rootTestApi = new RootApi();
                    String url1 = "https://open-api.yunjiai.cn/v1/robot/map/info?accessKeyId=dhz6Ra7Jo6qLD6me&deviceId=896002959698169856&signature=6clyx6skjzQcmwHECbNd8H8zCS8%3D&signatureNonce=1&storeId=202189105671016556095847096576&timestamp=2021-10-21T08:26:01Z";
                    String s1 = rootTestApi.loadJson(url1);
                    JSONObject rootjson = JSONObject.parseObject(s1);
                    JSONArray liststate = (JSONArray)  rootjson.get("data");
                    System.out.println(liststate);
                    RootMapList rootMapList = new RootMapList();
                    JsontoBean<RootMapList> objectJsontoBean = new JsontoBean<RootMapList>();
                    RootMapList tansfrom = objectJsontoBean.tansfrom(rootMapList, liststate);
                    System.out.println("tansfrom::"+tansfrom.getDeviceId());

                    /**
                     * 上传微瓴
                     */
                    JSONObject standardData = new JSONObject();
                    standardData.put("reportTs", System.currentTimeMillis());
                    JSONObject profile = new JSONObject();
                    profile.put("modelID", "200418");
                    profile.put("poiCode", "w0906002");
                    standardData.put("profile", profile);
                    WelinkHttp wh = new WelinkHttp(RegularInstance.registerData1, RegularInstance.loginData);
                    JSONObject properties = new JSONObject();
                    JSONObject propertie = new JSONObject();
                    propertie.put("getDeviceId", tansfrom.getDeviceId());
                    propertie.put("Floo", tansfrom.getFloor());
                    properties.put("mapinfo", propertie);
                    standardData.put("properties", properties);
                    //standardData.put("properties", "{}");
                    try {
                        //   System.out.println("上传数据");
                        String devRunEvent = JSONObject.toJSONString(standardData, SerializerFeature.WriteMapNullValue);
                        //   System.out.println("发送设备运行时事件数据==" + devRunEvent);
                        //    System.out.println("--------------------------------开始传输设备运行时事件数据");
                        wh.sendMessage(RegularInstance.loginData.load(), 80000, Long.parseLong(din), devRunEvent, true);
                    } catch (Exception e) {
                        System.out.println("设备运行时事件数据传输出现异常");
                    }


                }
                }
            }
        }
}